package io.halo.payment.modules.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.halo.payment.common.exception.HaloPaymentException;
import io.halo.payment.common.validator.Assert;
import io.halo.payment.modules.app.dao.HaloPaymentUserDao;
import io.halo.payment.modules.app.form.LoginForm;
import io.halo.payment.modules.app.model.HaloPaymentUser;
import io.halo.payment.modules.app.service.HaloPaymentUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;


@Service("userService")
public class HaloPaymentUserServiceImpl extends ServiceImpl<HaloPaymentUserDao, HaloPaymentUser> implements HaloPaymentUserService {

    @Override
    public HaloPaymentUser queryByMobile(String mobile) {
        return baseMapper.selectOne(new QueryWrapper<HaloPaymentUser>().eq("mobile", mobile));
    }

    @Override
    public long login(LoginForm form) {
        HaloPaymentUser user = queryByMobile(form.getMobile());
        Assert.isNull(user, "手机号或密码错误");

        //密码错误
        if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))) {
            throw new HaloPaymentException("手机号或密码错误");
        }

        return user.getUserId();
    }
}
